﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EC_DataBase;

namespace EasyCheque.Account
{
    public partial class TransactionQuery : System.Web.UI.Page
    {
        private void refreshGridView()
        {
            string whereClause = " WHERE ACCOUNT = '"
                + DropDownList_ACCOUNT_ID.SelectedValue.ToString() + "' ";

            System.Diagnostics.Debug.WriteLine("Start: " + TextBox_StartTime.Text + ", End: " + TextBox_EndTime.Text);

            if (!String.IsNullOrEmpty(TextBox_StartTime.Text))
            //if (TextBox_StartTime.Text != null)
            {
                whereClause += " AND TRANS_DATE > #" + DateTime.Parse(TextBox_StartTime.Text).ToString("yyyy-MM-dd") + "# ";
            }

            if (!String.IsNullOrEmpty(TextBox_EndTime.Text))
            //if (TextBox_EndTime.Text != null)
            {
                whereClause += " AND TRANS_DATE < #" + DateTime.Parse(TextBox_EndTime.Text).ToString("yyyy-MM-dd") + "# ";
            }

            System.Diagnostics.Debug.WriteLine("Where: " + whereClause);

            Session["whereClase"] = whereClause;
        }

        private bool haveGoodDates()
        {
            if (!String.IsNullOrEmpty(TextBox_StartTime.Text))
            {
                if (DateTime.Parse(TextBox_StartTime.Text) > DateTime.Now)
                {
                    Label_CommitMessage_StartTime.Visible = true;
                    Label_CommitMessage_StartTime.Text = "起始日期超出可信范围: " + TextBox_StartTime.Text;
                    return false;
                }
                else
                {
                    Label_CommitMessage_StartTime.Visible = false;
                    Label_CommitMessage_StartTime.Text = "";
                }
            }

            if (!String.IsNullOrEmpty(TextBox_EndTime.Text))
            {
                if (!String.IsNullOrEmpty(TextBox_StartTime.Text)
                    && (DateTime.Parse(TextBox_EndTime.Text) < DateTime.Parse(TextBox_StartTime.Text)))
                {
                    Label_CommitMessage_StartTime.Visible = true;
                    Label_CommitMessage_EndTime.Text = "结束日期在起始日期之前: " + TextBox_EndTime.Text;
                    return false;
                }
                else
                {
                    Label_CommitMessage_StartTime.Visible = false;
                    Label_CommitMessage_EndTime.Text = "";
                }
            }

            return true;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Page_Load");

            // 每次都会检查和处理的。

            //checkDates();
            //refreshGridView();

            if (IsPostBack)
            {
                return;
            }

            Panel_Calendar_StartTime.Style["display"] = "none";
            Panel_Calendar_StartTime.Style["position"] = "absolute";
            Panel_Calendar_EndTime.Style["display"] = "none";
            Panel_Calendar_EndTime.Style["position"] = "absolute";

            TextBox_StartTime.Text = DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd");
            TextBox_EndTime.Text = DateTime.Now.ToString("yyyy-MM-dd");

            // 先读入所有的AccountID。
            string[] allAccountIDs = OperateDB.Get_All_Acc_IDs(Convert.ToInt32(Session["uid"]));
            foreach (string accountID in allAccountIDs)
            {
                DropDownList_ACCOUNT_ID.Items.Add(accountID);
            }
        }

        protected void Calendar_InPanel_StartTime_SelectionChanged(object sender, EventArgs e)
        {
            Panel_Calendar_StartTime.Style["display"] = "none";
            TextBox_StartTime.Text = Calendar_InPanel_StartTime.SelectedDate.ToString("yyyy-MM-dd");
        }

        protected void Calendar_InPanel_StartTime_VisibleMonthChanged(Object sender, MonthChangedEventArgs e)
        {
            Panel_Calendar_StartTime.Style["display"] = "";
        }

        protected void Calendar_InPanel_EndTime_SelectionChanged(object sender, EventArgs e)
        {
            Panel_Calendar_EndTime.Style["display"] = "none";
            TextBox_EndTime.Text = Calendar_InPanel_EndTime.SelectedDate.ToString("yyyy-MM-dd");
        }

        protected void Calendar_InPanel_EndTime_VisibleMonthChanged(Object sender, MonthChangedEventArgs e)
        {
            Panel_Calendar_EndTime.Style["display"] = "";
        }

        protected void Button_CheckDetail_Click(object sender, EventArgs e)
        {
            if (!haveGoodDates())
            {
                return;
            }

            refreshGridView();
            Response.Redirect("~/Account/TransactionDetail.aspx");
        }
    }
}
